﻿// -----------------------------------------------------------------------
// <copyright file="RouteConfig.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

using System.Linq.Expressions;

namespace MBOP.Website
{
	#region Usings

	using System;
	using System.Collections.Generic;

	using System.Web.Mvc;
	using System.Web.Routing;
	using MBOP.Core.Entities.Common;
	using MBOP.Core.Entities.Library;

	#endregion

	/// <summary>
	/// Allows to configuring routes of the application
	/// </summary>
	public static class RouteConfig
	{
		/// <summary>
		/// Registers routes of the application.
		/// </summary>
		/// <param name="routes">Routes collection</param>
		public static void RegisterRoutes(RouteCollection routes)
		{
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			routes.MapRoute(null, string.Empty, new { controller = "Main", action = "Index" });

			routes.MapRoute(null, 
				"Library/{filterType}/{page}/{sortingField}/{sortingType}", 
				new { controller = "Library", action = "List", filterType = FilterType.All, page = 1, 
				sortingField = SortField.Default, sortingType = SortType.Default}, 
				new { page = "[1-9]{1,8}", filterType = GetEnumRouteConstraint(typeof(FilterType)), sortingField=GetEnumRouteConstraint(typeof(SortField)), sortingType=GetEnumRouteConstraint(typeof(SortType))});

			routes.MapRoute(null, 
				"Library/{action}", 
				new { controller = "Library" });

			routes.MapRoute(null, 
				"IdeaBoard/{action}/{page}", 
				new { controller = "IdeaBoard", action = "List", page = 1 }, 
				new { page = "[1-9]{1,8}" });

			routes.MapRoute(null, 
				"Photo/{action}/{page}", 
				new { controller = "Photo", action = "List", page = 1 }, 
				new { page = "[1-9]{1,8}" });

			routes.MapRoute(null, 
				"Error/{action}", 
				new { controller = "Error", action = "Index"});

			routes.MapRoute(null, 
				"{controller}/{action}/{ID}", 
				new { controller = "Image" });

			routes.MapRoute(null, 
				"{controller}/{action}");
		}

		/// <summary>
		/// Gets the filter route constraint.
		/// </summary>
		/// <returns>The filter route constraint.</returns>
		private static string GetEnumRouteConstraint(Type enumType)
		{
			List<string> filterTypeElements = new List<string>();

			foreach (var element in Enum.GetValues(enumType))
			{
				filterTypeElements.Add(element.ToString());
			}

			return string.Join("|", filterTypeElements);
		}
	}
}